<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use \App\Post;
use \App\Comment;
use \App\Zan;

class PostController extends Controller
{

    //列表
    public function index()
    {
        //把所有的数据按照create_at时间降序的方式显示出来
//        $posts = Post::orderBy('created_at', 'desc')->get();

        //分页显示
        $posts = Post::orderBy('created_at', 'desc')->withCount(["comment", 'zans'])->Paginate(6);
        return view('post/index', compact('posts'));
    }

    //详细
    public function show(Post $post)
    {
        $post->load('comment');
        return view('post/show', compact('post'));
    }

    //创建
    public function create()
    {
        return view('post/create');
    }

    //创建
    public function store()
    {


        //会出错，及时从头content设置的最小长度为10，而且当小于5的
        //时候会提示错误，长度应该大于10，但是当大于等于5时就认为是
        //正确的
        //验证
        $this->validate(request(), [
            'title' => 'required|string|max:100|min:5',
            'content' => 'required|string|min:10'
        ]);

        //错误提示信息本地化的一种方法
        /*$this->validate(request(), [
            'title' => 'required|string|max:100|min:5',
            'content' => 'required|string|min:10'
        ], [
            'title.min' => '文章标题过短'
        ]);*/

        //保存数据到数据库中
        //方法一:
//        $post = new Post();
//        $post->title = request('title');
//        $post->content = request('content');
//        $post.save();
        //方法二:
//        $params = ['title' => request('title'), 'content' => request('content')];
        //或者 $params=request(['title','content']);

        //逻辑
        $user_id = \Auth::id();
        $params = array_merge(\request(['title', 'content']), compact('user_id'));
        $post = Post::create($params);

        //打印所有的信息
//        dd(request());
        //打印请求的信息
//        dd($post);

        //渲染
        return redirect('/posts');
    }

    //上传图片
    public function imageUpload(Request $request)
    {
        $path = $request->file('wangEditorH5File')->storePublicly(md5(time()));
        return asset('storage/' . $path);
//        dd(\request()->all());
    }

    //编辑
    public function edit(Post $post)
    {
        return view('post/edit', compact('post'));
    }

    //更新
    public function update(Post $post)
    {
        //验证
        $this->validate(request(), [
            'title' => 'required|string|max:100|min:5',
            'content' => 'required|string|min:10'
        ]);

        $this->authorize('update', $post);
        //逻辑
        $post->title = \request('title');
        $post->content = \request('content');
        $post->save();
        //渲染
        return redirect("/posts/{$post->id}");

    }

    //删除
    public function delete(Post $post)
    {
        //用户权限认证
        $this->authorize('delete', $post);

        $post->delete();
        return redirect("/posts");
    }

    //提交评论
    public function comment(Post $post)
    {
        //验证
        $this->validate(request(), [
            'content' => 'required|min:3',
        ]);
        //逻辑
        $comment = new Comment();
        $comment->user_id = \Auth::id();
        $comment->content = \request('content');
        $post->comment()->save($comment);
        //渲染
        return back();

    }

    public function zan(Post $post)
    {
        $param = [
            'user_id' => \Auth::id(),
            'post_id' => $post->id,

        ];
        Zan::firstOrCreate($param);
        return back();
    }

    public function unzan(Post $post)
    {
        $post->zan(\Auth::id())->delete();
        return back();

    }

    public function search()
    {
        //验证
        $this->validate(\request(), [
            'query' => 'required'
        ]);
        //逻辑
        $query = \request('query');
        $posts = \App\Post::search($query)->paginate(2);
        //渲染
        return view("post/search", compact('posts', 'query'));
    }

}
